package com.wss.common.utils;


import cn.hutool.core.date.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    // 获得周一的日期
    public static String getMonday(String d) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式  
        Calendar cal = Calendar.getInstance();
        Date time=sdf.parse(d);
        cal.setTime(time);
        //判断要计算的日期是否是周日，如果是则返回
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天  
        if(1 == dayWeek) {
            dayWeek = 8;
        }
        cal.add(Calendar.DATE, 2 - dayWeek);
        return sdf.format(cal.getTime());
    }


    public  static  Date getDate(LocalDateTime date){
        return Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
    }

    public  static  Date getDate(Long date){
        return new  Date(date);
    }

    public  static  Long getDateLong(String date,String parse){
        DateTime time = cn.hutool.core.date.DateUtil.parse(date, parse);
        return time.getTime();
    }



    public  static  String  getDateStr(LocalDateTime date,String format) {
        String s = date.format(DateTimeFormatter.ofPattern(format));
        return s;
    }

    public  static  String  getDateStr(Long date,String format) {
        Date date1 = getDate(date);
        return getDateStr(date1,format);
    }

    public  static  String  getDateStr(LocalDate date, String format) {
        String s = getDateStr(date.atStartOfDay(ZoneId.systemDefault()).toLocalDateTime(),format);
        return s;
    }

    public  static  String  getDateStr(Date date,String format) {
        String s = cn.hutool.core.date.DateUtil.format(date,format);
        return s;
    }

    /**
     *  毫秒 时间戳
     * @param date
     * @return
     */
    public  static  Long getTimeSpan(Date date){
        return date.toInstant().toEpochMilli();
    }

    /**
     * 毫秒 时间戳
     * @param date
     * @return
     */
    public  static  Long getTimeSpan(LocalDateTime date){
        return getDate(date).toInstant().toEpochMilli();
    }

    /**
     * 毫秒 时间戳
     * @param date
     * @return
     */
    public  static  Long getTimeSpan(LocalDate date){
        return getDate(date.atStartOfDay(ZoneId.systemDefault()).toLocalDateTime()).toInstant().toEpochMilli();
    }
}
